#include <stdio.h>

int BSearch(int a[], int x, int low, int high) {
    if (low > high) return -1; //待搜区间为空
    else {
        int mid = (low + high) / 2;
        if (x == a[mid]) return mid;  //搜索成功
        if (x < a[mid])      //在左半区间搜索
            return BSearch(a, x, low, mid - 1);
        else    //在右半区间搜索
            return BSearch(a, x, mid + 1, high);
    }
}

int main() {
    int a[] = {0,1, 2, 3, 4};
    int x = 1;
    int index = BSearch(a, x, 0, 4);
    printf("index = %d\n", index);
    return 0;
}